Example for <c:import>
Given below is an example usage of the <c:import> tag which will help you to understand it in more detail.
HTML
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> < html > < head > < title >Tag Example</ title > </ head > < body > < c:import var = "data" url = "https://www.w3wiki.org" /> < c:out value = "${data}" /> </ body > </ html > |
Output:
In the example above, the entire content would be fetched from w3wiki.org and stored in the “data” variable, which will later be printed.
Note:
You only need the URI relative to the web app root if you’re importing content from the same web application. The whole URL is not required.
Example
<c:import url="/menu.jsp" charEncoding="UTF-8" />
JSTL Core Tag
JavaServer Pages (JSP) is a technology through which developers can create dynamic, server-side web applications. The Java code can be easily integrated into HTML templates to generate dynamic content. The JavaServer Pages Standard Tag Library (JSTL), is one of the main components of JSP. The <c:import> tag is one of the several tags offered by JSTL that is used for embedding external content into a JSP page. In this article, we will understand the JSTL Core <c:import> tag in detail.
The <c:import> tag is a useful tool for including external content, such as HTML or JSP files, within a JSP page. You can directly import and embed content from an external source or websites directly into your JSP. As a result, a handy feature for creating modular, reusable, and dynamic web pages can be developed.
Syntax for <c:import>
The syntax for the <c:import> tag contains multiple parameters.
<c:import
url="string"
var="string"
scope ="string"
varRender ="string"
context ="string"
charEncoding="string"
/>
Attributes of <c:import>
All Associated Attributes with JSTL Core <c:import> Tag are listed below:
Name | Required | Type | Default | Description |
---|---|---|---|---|
url | Yes | java.lang.String | None | The local file’s or website’s URL for import. |
var | No | java.lang.String | Print to Page | The variable’s name that contains the imported information. |
scope | No | java.lang.String | Page | Scope of the variable. |
varRender | No | java.lang.String | None | The variable’s name that will make the imported content visible is Reader. The type of this variable is java.io.Reader. |
context | No | java.lang.String | Current application | Context information about a web application while interacting with a related resource that is part of an external context. |
charEncoding | No | java.lang.String | ISO-8859-1 | The imported content’s character encoding. |
Contact Us